package com.attendance.www.hr.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.attendance.www.hr.beans.AnnouncementBean;
import com.attendance.www.hr.beans.AnnouncementPageBean;
import com.attendance.www.hr.beans.AttachmentBean;
import com.attendance.www.hr.dao.mapper.AnnouncementAttachmentMapper;
import com.attendance.www.hr.dao.mapper.AnnouncementExtMapper;
import com.attendance.www.hr.dao.mapper.AnnouncementMapper;
import com.attendance.www.hr.dao.mapper.AttachmentMapper;
import com.attendance.www.hr.entitys.Announcement;
import com.attendance.www.hr.entitys.AnnouncementAttachmentExample;
import com.attendance.www.hr.entitys.AnnouncementAttachmentKey;
import com.attendance.www.hr.entitys.AnnouncementExample;
import com.attendance.www.hr.entitys.Attachment;
import com.attendance.www.hr.entitys.AttachmentExample;
import com.attendance.www.hr.exception.ResultFaultException;
import com.attendance.www.hr.service.IAnnouncementService;

@Service
public class AnnouncementServiceImpl implements IAnnouncementService {

	@Resource
	private AnnouncementMapper announcementDao;
	@Resource
	private AnnouncementExtMapper announcementExtDao;
	@Resource
	private AnnouncementAttachmentMapper announcementAttachmentDao;
	@Resource
	private AttachmentMapper attachmentDao;

	/**
	 * 新增公告
	 */
	@Override
	public boolean saveAnnouncement(AnnouncementBean announcementBean) {
		AnnouncementExample announcementExample = new AnnouncementExample();
		announcementExample.createCriteria().andTitleEqualTo(announcementBean.getTitle()).andIsDeleteEqualTo(0);
		List<Announcement> list = announcementDao.selectByExample(announcementExample);
		if (list.size() > 0) {
			throw new ResultFaultException("0_00010");
		}
		Announcement announcement = new Announcement();
		announcement.setTitle(announcementBean.getTitle());
		announcement.setContent(announcementBean.getContent());
		announcement.setExpireDate(announcementBean.getExpireDate());
		int record = announcementDao.insertSelective(announcement);
		if (record > 0) {
			// 附件保存
			for (AttachmentBean attachmentBean : announcementBean.getAttachmentList()) {
				AnnouncementAttachmentKey announcementAttachmentKey = new AnnouncementAttachmentKey();
				announcementAttachmentKey.setAnnouncementId(announcement.getId());
				announcementAttachmentKey.setAttachmentId(attachmentBean.getAttachmentId());
				announcementAttachmentDao.insertSelective(announcementAttachmentKey);
			}
			return true;
		}
		return false;
	}

	/**
	 * 删除公告
	 */
	@Override
	public boolean deleteAnnouncement(String id) {
		Announcement announcement = new Announcement();
		announcement.setId(Integer.parseInt(id));
		announcement.setIsDelete(1);
		int record = announcementDao.updateByPrimaryKeySelective(announcement);
		if (record > 0) {
			// 先删除后添加
			AnnouncementAttachmentExample announcementAttachmentExample = new AnnouncementAttachmentExample();
			announcementAttachmentExample.createCriteria().andAnnouncementIdEqualTo(announcement.getId());
			announcementAttachmentDao.deleteByExample(announcementAttachmentExample);
			return true;
		}
		return false;
	}

	/**
	 * 修改公告
	 */
	@Override
	public boolean updateAnnouncement(AnnouncementBean announcementBean) {
		AnnouncementExample announcementExample = new AnnouncementExample();
		announcementExample.createCriteria().andTitleEqualTo(announcementBean.getTitle())
				.andIdNotEqualTo(announcementBean.getId()).andIsDeleteEqualTo(0);
		List<Announcement> list = announcementDao.selectByExample(announcementExample);
		if (list.size() > 0) {
			throw new ResultFaultException("0_00010");
		}
		Announcement announcement = new Announcement();
		announcement.setId(announcementBean.getId());
		announcement.setTitle(announcementBean.getTitle());
		announcement.setContent(announcementBean.getContent());
		announcement.setExpireDate(announcementBean.getExpireDate());
		int record = announcementDao.updateByPrimaryKeySelective(announcement);
		if (record > 0) {
			// 先删除后添加
			AnnouncementAttachmentExample announcementAttachmentExample = new AnnouncementAttachmentExample();
			announcementAttachmentExample.createCriteria().andAnnouncementIdEqualTo(announcement.getId());
			announcementAttachmentDao.deleteByExample(announcementAttachmentExample);

			// 附件保存
			for (AttachmentBean attachmentBean : announcementBean.getAttachmentList()) {
				AnnouncementAttachmentKey announcementAttachmentKey = new AnnouncementAttachmentKey();
				announcementAttachmentKey.setAnnouncementId(announcement.getId());
				announcementAttachmentKey.setAttachmentId(attachmentBean.getAttachmentId());
				announcementAttachmentDao.insertSelective(announcementAttachmentKey);
			}
			return true;
		}
		return false;
	}

	/**
	 * 查询公告(管理员界面)
	 */
	@Override
	public AnnouncementPageBean getAnnouncementList(AnnouncementBean announcementBean) {
		AnnouncementPageBean announcementPageBean = new AnnouncementPageBean();
		announcementPageBean.setAnnouncementBeanList(announcementExtDao.getAnnouncementList(announcementBean));
		announcementPageBean.setPageTotal(announcementExtDao.count(announcementBean));
		return announcementPageBean;
	}

	/**
	 * ID查询公告栏
	 * 
	 * @param id
	 * @return
	 */
	@Override
	public AnnouncementBean getAnnouncementById(Integer id) {
		AnnouncementBean announcementBean = new AnnouncementBean();
		Announcement announcement = announcementDao.selectByPrimaryKey(id);
		if (announcement != null) {
			announcementBean.setId(id);
			announcementBean.setTitle(announcement.getTitle());
			announcementBean.setContent(announcement.getContent());
			announcementBean.setExpireDate(announcement.getExpireDate());
			// 查询附件
			List<AttachmentBean> attachmentBeanList = new ArrayList<AttachmentBean>();
			AnnouncementAttachmentExample announcementAttachmentExample = new AnnouncementAttachmentExample();
			announcementAttachmentExample.createCriteria().andAnnouncementIdEqualTo(id);
			List<AnnouncementAttachmentKey> announcementAttachmentKeyList = announcementAttachmentDao
					.selectByExample(announcementAttachmentExample);
			if (announcementAttachmentKeyList.size() > 0) {
				List<Integer> attachmentIdList = announcementAttachmentKeyList.stream()
						.map(AnnouncementAttachmentKey::getAttachmentId).collect(Collectors.toList());
				if (attachmentIdList != null) {
					AttachmentExample attachmentExample = new AttachmentExample();
					attachmentExample.createCriteria().andIdIn(attachmentIdList);
					List<Attachment> attachmentList = attachmentDao.selectByExample(attachmentExample);
					for (Attachment attachment : attachmentList) {
						AttachmentBean attachmentBean = new AttachmentBean();
						attachmentBean.setAttachmentId(attachment.getId());
						attachmentBean.setFileName(attachment.getFileName());
						attachmentBean.setUrl(attachment.getUrl());
						attachmentBeanList.add(attachmentBean);
					}
				}
			}
			announcementBean.setAttachmentList(attachmentBeanList);
		}
		return announcementBean;
	}

	/**
	 * 停用公告
	 */
	@Override
	public boolean isDeactivateAnnouncement(String id, String isDeactivate) {
		Announcement announcement = new Announcement();
		announcement.setId(Integer.parseInt(id));
		announcement.setIsDeactivate(Integer.parseInt(isDeactivate));
		int record = announcementDao.updateByPrimaryKeySelective(announcement);
		if (record > 0) {
			return true;
		}
		return false;
	}

	/**
	 * 查询公告(用户界面)
	 */
	@Override
	public List<AnnouncementBean> getAnnouncementListByExpireDate() {
		List<AnnouncementBean> announcementList = announcementExtDao.getAnnouncementListByExpireDate();
		// 查询附件
		for(AnnouncementBean announcementBean:announcementList) {
			// 查询附件
			List<AttachmentBean> attachmentBeanList = new ArrayList<AttachmentBean>();
			AnnouncementAttachmentExample announcementAttachmentExample = new AnnouncementAttachmentExample();
			announcementAttachmentExample.createCriteria().andAnnouncementIdEqualTo(announcementBean.getId());
			List<AnnouncementAttachmentKey> announcementAttachmentKeyList = announcementAttachmentDao
					.selectByExample(announcementAttachmentExample);
			if (announcementAttachmentKeyList.size() > 0) {
				List<Integer> attachmentIdList = announcementAttachmentKeyList.stream()
						.map(AnnouncementAttachmentKey::getAttachmentId).collect(Collectors.toList());
				if (attachmentIdList != null) {
					AttachmentExample attachmentExample = new AttachmentExample();
					attachmentExample.createCriteria().andIdIn(attachmentIdList);
					List<Attachment> attachmentList = attachmentDao.selectByExample(attachmentExample);
					for (Attachment attachment : attachmentList) {
						AttachmentBean attachmentBean = new AttachmentBean();
						attachmentBean.setAttachmentId(attachment.getId());
						attachmentBean.setFileName(attachment.getFileName());
						attachmentBean.setUrl(attachment.getUrl());
						attachmentBeanList.add(attachmentBean);
					}
				}
			}
			announcementBean.setAttachmentList(attachmentBeanList);
		}
		return announcementList;
	}
}
